<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/3/22
 * Time: 15:15
 */

namespace app\index\controller;


use app\index\model\Role;
use app\index\model\UserRoleRelation;
use app\index\validate\UserValidate;
use think\Db;

class User extends Common
{
    /**
     * 页面初始展示
     * @return mixed|\think\response\Json
     */
    public function index()
    {
        if ($this->request->isAjax()){
            $model = new \app\index\model\User();

            $page = $this->request->param()["page"];
            $limit = $this->request->param()["limit"];

            $data["data"] = $model->whereNotIn("name",["admin","root"])->page($page)->limit($limit)->select();
            foreach ( $data["data"] as $key =>  $value) {
                $data["data"][$key]["last_login_time"] = date("Y-m-d H:i:s",$data["data"][$key]["last_login_time"]);
                $data["data"][$key]["job"] = $value["getRoleId"][0]["name"];
            }
            $data["count"] = $model->whereNotIn("name",["admin","root"])->count();
            $data["code"] = 0;
            return json($data);
        }
        return $this->fetch();
    }

    /**
     * 修改
     * @return mixed|\think\response\Json
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        if($this->request->isAjax() && $this->request->isPost()) {
            $data = $this->request->param();
            $data["salt"] = rand(000000,999999);
            $data["password"] =  password_hash($data["password"] . $data["salt"],1);

            $model = new \app\index\model\User();
            $model->startTrans();

            $result = $model->where("id",$data["id"])->update(["password" => $data["password"],"salt" => $data["salt"]]);
            if (!$result){ $model->rollback(); return json(["code" => 0,"msg" => "修改失败"]);}

            $result = UserRoleRelation::where("user_id",$data["id"])->update(["role_id" => $data["city"],"update_time" => time()]);
            if (!$result){ $model->rollback(); return json(["code" => 0,"msg" => "修改失败"]);}


            $model->commit();
            return json(["code" => 1,"msg" => "修改成功"]);
        }
        $data = $this->request->param();
        $data = \app\index\model\User::get($data["id"]);
        $data["role_id"] = UserRoleRelation::where("user_id",$data["id"])->value("role_id");
        $this->assign("data",$data);
        $job = Role::select();
        $this->assign("job",$job);
        return  $this->fetch();
    }



    /**
     * 删除管理人员账号
     * @return \think\response\Json
     * @throws \Exception
     */
    public function del()
    {
        if($this->request->isAjax() && $this->request->isPost()) {
            $data = $this->request->param();

            $model = new \app\index\model\User();
            $model->startTrans();

            $result = UserRoleRelation::where("user_id",$data["id"])->delete();
            if (!$result){ $model->rollback(); return json(["code" => 0,"msg" => "删除失败"]);}

            $result = $model->where("id",$data["id"])->delete();
            if (!$result){ $model->rollback(); return json(["code" => 0,"msg" => "删除失败"]);}

            $model->commit();
            return json(["code" => 1,"msg" => "删除成功"]);
        }
    }

    /**
     * 添加新管理账号
     * @return mixed|\think\response\Json
     */
    public function add()
    {
        if($this->request->isAjax() && $this->request->isPost()) {
            $data = $this->request->param();

            $validate = new UserValidate();

            if( $result = $validate->scene("UserAdd")->check($data)) return json(["code" => 0,"msg" => "用户已存在"]);

            $data["salt"] = rand(000000,999999);
            $data["password"] =  password_hash($data["password"] . $data["salt"],1);
            $data["last_login_time"] = time();
            $data["last_login_ip"] = $this->request->ip();

            $db = new Db();
            $db::startTrans();

            $result = (new \app\index\model\User())->allowField(true)->save($data);
            if (!$result) { $db::rollback();  return json(["code" => 0,"msg" => "用户添加失败"]);}


            $id = (new \app\index\model\User())->getLastInsID();
            $result =  $db::table("user_role_relation")->insert(["user_id" => $id,"role_id" => $data["city"],"update_time" => time()]);
            if (!$result) { $db::rollback();  return json(["code" => 0,"msg" => "用户添加失败"]);}


            $db::commit();
            return json(["code" => 1,"msg" => "用户已添加"]);
        }

        $job = Role::select();
        $this->assign("job",$job);
        return  $this->fetch();
    }


}